const ModelSinger = require('../../../models/music/singer'),
    FileTool = require('../../../utils/filetool'),
    ComTool = require('../../../utils/commonTools');

const fileSavePath = "/images/singers_avatar_url";
// 删除歌手
exports.delSinger = (req, res, next) => {
    const { singer_id, singer_pic, singer_avatar } = req.query;
    ModelSinger.delSinger({ singer_id }
        , (value) => {
            if (singer_pic && singer_avatar)
                FileTool.removeFile('./public' + singer_pic, singer_avatar);
            res.render('success', {
                data: JSON.stringify({
                    message: "删除成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "删除失败",
                    status: 200,
                    error
                })
            })
        });
}
// 编辑歌手
exports.updateSinger = async (req, res, next) => {
    const { singer, file } = req.body;
    let newData = file ? await FileTool.saveImage(file, './public' + fileSavePath) : null;
    if (newData === false) {
        return res.render('fail', {
            data: JSON.stringify({
                message: "图片保存失败",
                status: 200,
            })
        })
    } else {
        if (singer.singer_pic && singer.singer_avatar)
            FileTool.removeFile('./public' + singer.singer_pic, singer.singer_avatar);
    }

    let Data = {
        ...singer,
        singer_pic: newData ? fileSavePath + '/' + newData.saveFloder + '/' : null,
        singer_avatar: newData ? newData.name : null,
    }

    const { singer_id } = Data;
    if (singer_id) {
        delete Data.singer_id;
        ModelSinger.updateSinger(Data, {
            singer_id
        }, (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "更新成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "更新失败",
                    status: 200,
                    error
                })
            })
        });
    }
}

/** 添加歌手 */
exports.addSinger = (req, res, next) => {
    ModelSinger.addSinger({
        ...req.body,
        singer_id: ComTool.orderCode()
    },
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "添加成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "添加失败",
                    status: 200,
                    error
                })
            })
        });
}
/** 查找歌手 */
exports.findSinger = async (req, res, next) => {
    let Singer = await ModelSinger.findSinger(req.body);
    Singer.success((value) => {
        if (value.length) {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找到歌手",
                    value: value,
                    status: 200,
                    length: value.length
                })
            })
        } else {
            res.render('fail', {
                data: JSON.stringify({
                    message: "没有找到歌手",
                    value: value,
                    length: value.length
                })
            })
        }
    });

    Singer.fail((error) => {
        res.render('fail', {
            data: JSON.stringify({
                message: "查找失败",
                error
            })
        })
    })
}
//查找所有歌手
exports.list = (req, res, next) => {
    const { isAll } = req.body;
    
    ModelSinger.findAll(
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找成功",
                    status: 200,
                    value: value,
                    length: value.length
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "查找失败",
                    status: 200,
                    error
                })
            })
        }, isAll);
}